package com.kunya.common.filter;



import com.kunya.common.common.Const;
import com.kunya.common.po.User;
import com.kunya.common.util.CookieUtil;
import com.kunya.common.util.JsonUtil;
import com.kunya.common.util.RedisShardedPoolUtil;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**   
* @Description:重置session时间filter
* @author zp  
* @date 2018年4月29日  
*/  
public class SessionExpireFilter implements Filter {

	@Override
	public void destroy() {
	//销毁	
	}

	@Override
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
			throws IOException, ServletException {
		HttpServletRequest httpServletRequest=(HttpServletRequest)servletRequest;
		//将表前缀放入request中
		String serverName=httpServletRequest.getServerName();
		//重置redis中用户session有效期
		String loginToken= CookieUtil.readLoginToken(httpServletRequest);
		if(StringUtils.isNotEmpty(loginToken)) {
			//判断loginToken是否为空或空字符串，如果不为空从redis中取user信息
			String userJsonStr= RedisShardedPoolUtil.get(loginToken);
			User user= JsonUtil.string2Obj(userJsonStr, User.class);
			if(user!=null) {
				//如果从redis中取出用户信息，重置loginToken有效期
				RedisShardedPoolUtil.expire(loginToken, Const.RedisCacheExtime.REDIS_SESSION_EXTIME);
			}
			
		}
		
		
	filterChain.doFilter(httpServletRequest, servletResponse);
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
   //初始化		
	}

}
